package com.machine.filling.util;

import java.text.DecimalFormat;
import java.util.Locale;

/**
 * Created by 86178 on 2019/7/15.
 * author mogen
 * 用心，成就一切。
 */

public class NumberUtil {

    public static byte[] sendOilGunBytes(byte[] bytes) {
        byte[] byteResult = new byte[18];
        byteResult[0] = (byte)0x7E;
        byteResult[1] = (byte)0xFE;
        byteResult[2] = (byte)0x10;
        byteResult[3] = (byte)0x7E;
        byteResult[4] = (byte)0x21;
        byteResult[5] = (byte)0x04;
        byteResult[6] = (byte)0x0B;
        byteResult[7] = 0x00;
        byteResult[8] = 0x00;
        byteResult[9] = bytes[13];
        byteResult[10] = bytes[14];
        byteResult[11] = bytes[15];
        byteResult[12] = 0x00;
        byteResult[13] = bytes[10];
        byteResult[14] = bytes[11];
        byteResult[15] = bytes[12];
        byteResult[16] = bytes[8];
        byteResult[17] = bytes[9];
        return byteResult;
    }

    public static byte[] sendOilGunBytesThird(byte[] gun) {
        byte[] guns = sendOilGunBytes(gun);
        byte[] crcs = NumberUtil.hexStrTobytes(CrcUtil.getCRC16(guns));
        byte[] byteResult = new byte[21];
        byteResult[0] = guns[0];
        byteResult[1] = guns[1];
        byteResult[2] = guns[2];
        byteResult[3] = guns[3];
        byteResult[4] = guns[4];
        byteResult[5] = guns[5];
        byteResult[6] = guns[6];
        byteResult[7] = guns[7];
        byteResult[8] = guns[8];
        byteResult[9] = guns[9];
        byteResult[10] = guns[10];
        byteResult[11] = guns[11];
        byteResult[12] = guns[12];
        byteResult[13] = guns[13];
        byteResult[14] = guns[14];
        byteResult[15] = guns[15];
        byteResult[16] = guns[16];
        byteResult[17] = guns[17];
        byteResult[18] = crcs[0];
        byteResult[19] = crcs[1];
        byteResult[20] = (byte)0x0D;
        return byteResult;
    }

    /**
     * gbk转UTF-8
     * @param gbkStr
     * @return
     */
    public static byte[] getUTF8BytesFromGBKString(String gbkStr) {
        int n = gbkStr.length();
        byte[] utfBytes = new byte[3 * n];
        int k = 0;
        for (int i = 0; i < n; i++) {
            int m = gbkStr.charAt(i);
            if (m < 128 && m >= 0) {
                utfBytes[k++] = (byte) m;
                continue;
            }
            utfBytes[k++] = (byte) (0xe0 | (m >> 12));
            utfBytes[k++] = (byte) (0x80 | ((m >> 6) & 0x3f));
            utfBytes[k++] = (byte) (0x80 | (m & 0x3f));
        }
        if (k < utfBytes.length) {
            byte[] tmp = new byte[k];
            System.arraycopy(utfBytes, 0, tmp, 0, k);
            return tmp;
        }
        return utfBytes;
    }

    /**
     * int保留两位小数
     *
     * @param a
     * @return
     */
    public static String saveTwo(int a) {
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format((float) a / 100);
    }

    /**
     * 十六进制字符串装十进制
     *
     * @param hex 十六进制字符串
     * @return 十进制数值
     */
    public static int hexStringToAlgorism(String hex) {
        hex = hex.toUpperCase();
        int max = hex.length();
        int result = 0;
        for (int i = max; i > 0; i--) {
            char c = hex.charAt(i - 1);
            int algorism = 0;
            if (c >= '0' && c <= '9') {
                algorism = c - '0';
            } else {
                algorism = c - 55;
            }
            result += Math.pow(16, max - i) * algorism;
        }
        return result;
    }


    /**
     * 16进制转10进制
     * Integer.parseInt()转换方法总结
     * parseInt("0", 10) returns 0
     * parseInt("473", 10) returns 473
     * parseInt("+42", 10) returns 42
     * parseInt("-0", 10) returns 0
     * parseInt("-FF", 16) returns -255
     * parseInt("1100110", 2) returns 102
     * parseInt("2147483647", 10) returns 2147483647
     * parseInt("-2147483648", 10) returns -2147483648
     * parseInt("2147483648", 10) throws a NumberFormatException
     * parseInt("99", 8) throws a NumberFormatException
     * parseInt("Kona", 10) throws a NumberFormatException
     * parseInt("Kona", 27) returns 411787
     *
     * @param hex
     * @return
     */

    public static int hex2decimal(String hex) {
        return Integer.parseInt(hex, 16);
    }

    /**
     * 将 int 类型数据转成二进制的字符串，不足 int 类型位数时在前面添“0”以凑足位数
     *
     * @param num
     * @return
     */
    public static String toFullBinaryString(int num) {
        char[] chs = new char[Integer.SIZE];
        for (int i = 0; i < Integer.SIZE; i++) {
            chs[Integer.SIZE - 1 - i] = (char) (((num >> i) & 1) + '0');
        }
        return new String(chs);
    }

    /**
     * byte转二进制
     *
     * @param b
     * @return
     */
    public static String sixteenChangeToTwo(byte b) {
        return Integer.toBinaryString(byteChangeToTen(b));
    }

    /**
     * 二进制的字符串转换为十进制
     *
     * @param aa
     */
    public static int twoChangeToTen(String aa) {
        return Integer.parseInt(aa, 2);
    }

    /**
     * @return 将byte转化为String
     */
    public static String getStringByByte(byte b) {
        return String.format("%02X", b & 0xff);
    }

    /**
     * 比较两个byte数组数据是否相同,相同返回 true
     *
     * @param data1
     * @param data2
     * @param len
     * @return
     */
    public static boolean equalByteMethod(byte[] data1, byte[] data2, int len) {
        if (data1 == null && data2 == null) {
            return true;
        }
        if (data1 == null || data2 == null) {
            return false;
        }
        if (data1 == data2) {
            return true;
        }
        boolean bEquals = true;
        int i;
        for (i = 0; i < data1.length && i < data2.length && i < len; i++) {
            if (data1[i] != data2[i]) {
                bEquals = false;
                break;
            }
        }
        return bEquals;
    }

    /**
     * byte 数组  转 16进制字符串
     *
     * @param src
     * @return
     */
    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }


    /**
     * 把16进制字符串转换成byte[]
     * @param src
     * @return
     */
    public static byte[] hexStr2Bytes(String src){
        /*对输入值进行规范化整理*/
        src = src.trim().replace(" ", "").toUpperCase(Locale.US);
        //处理值初始化
        int m=0,n=0;
        int iLen=src.length()/2; //计算长度
        byte[] ret = new byte[iLen]; //分配存储空间

        for (int i = 0; i < iLen; i++){
            m=i*2+1;
            n=m+1;
            ret[i] = (byte)(Integer.decode("0x"+ src.substring(i*2, m) + src.substring(m,n)) & 0xFF);
        }
        return ret;
    }

    /**
     * 将byte[]转换成0x01,0x02等格式的数据
     * @param data
     * @return
     */
    public static String showDataDetail(byte[] data){
        final StringBuilder stringBuilder = new StringBuilder(data.length);
        for(byte bytechar:data){
            stringBuilder.append(String.format("%02x",bytechar));
        }
        return stringBuilder.toString();
    }

    /**
     * 16进制字符串 转byte 数组
     *
     * @param hexStrs
     * @return
     */
    public static byte[] hexStrTobytes(String hexStrs) {
        byte[] bytes = new byte[hexStrs.length() / 2];
        if (hexStrs.length() % 2 != 0) {
            LogUtil.e("hexStrTobytes() returned: " + "命令帧错误");
            return bytes;
        }
        for (int i = 0; i < hexStrs.length() / 2; i++) {
            bytes[i] = (byte) Integer.parseInt(hexStrs.substring(i * 2, i * 2 + 2), 16);
        }
        return bytes;
    }

    /**
     * int转byte数组
     *
     * @return
     */
    public static byte[] IntToByte(int num) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) ((num >> 24) & 0xff);
        bytes[1] = (byte) ((num >> 16) & 0xff);
        bytes[2] = (byte) ((num >> 8) & 0xff);
        bytes[3] = (byte) (num & 0xff);
        return bytes;
    }

    /**
     * 16进制转换10进制
     *
     * @param b
     * @return
     */
    public static int byteChangeToTen(byte b) {
        int shift = (3 - b) * 8;
        return (b & 0xFF) << shift;
    }

    /**
     * 字节数组转成16进制表示格式的字符串
     *
     * @param byteArray
     *            需要转换的字节数组
     * @return 16进制表示格式的字符串
     **/
    public static String toHexString(byte[] byteArray) {
        if (byteArray == null || byteArray.length < 1)
            throw new IllegalArgumentException("this byteArray must not be null or empty");

        final StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < byteArray.length; i++) {
            if ((byteArray[i] & 0xff) < 0x10)//0~F前面不零
                hexString.append("0");
            hexString.append(Integer.toHexString(0xFF & byteArray[i]));
        }
        return hexString.toString().toLowerCase();
    }

}
